package com.example.questionnaire.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionnaire.entity.AnswererEntity;
import com.example.questionnaire.entity.AnswererTeamEntity;
import com.example.questionnaire.entity.UserEntity;
import com.example.questionnaire.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.questionnaire.dao.UserTeamDao;
import com.example.questionnaire.entity.UserTeamEntity;
import com.example.questionnaire.service.UserTeamService;


@Service("userTeamService")
public class UserTeamServiceImpl extends ServiceImpl<UserTeamDao, UserTeamEntity> implements UserTeamService {

    private UserTeamDao userTeamDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserTeamEntity> page = this.page(
                new Query<UserTeamEntity>().getPage(params),
                new QueryWrapper<UserTeamEntity>()
        );
        page.setTotal(page.getRecords().size());
        return new PageUtils(page);
    }
    @Autowired
    UserTeamService userTeamService;
    @Override
    public PageUtils queryPageUserInTeam(Map<String, Object> params) {
        QueryWrapper wrapper = new QueryWrapper();
        if(params.get("id") != null){
            wrapper.eq("id",params.get("id"));

        }
        IPage<UserTeamEntity> page = this.page(
                new Query<UserTeamEntity>().getPage(params),
                new QueryWrapper<UserTeamEntity>()
        );

        return new PageUtils(page);
    }
    @Autowired
    UserService userService;
    @Override
    public PageUtils queryUsersInTeam(Map<String, Object> params) {
        QueryWrapper<UserTeamEntity>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("team_id",params.get("teamId"));
        List<UserTeamEntity> list = this.list(queryWrapper);

        QueryWrapper<UserEntity> wrapper = new QueryWrapper<UserEntity>();
        if(!list.isEmpty()) {
            List<Integer> collect = list.stream().map(item -> {
                Integer userId = item.getUserId();
                wrapper.or(item1->{
                    item1.eq("id",userId).eq("is_deleted",0);
                });
                return userId;
            }).collect(Collectors.toList());
            IPage<UserEntity> page = userService.page(
                    new Query<UserEntity>().getPage(params),
                    wrapper
            );

            return new PageUtils(page);
        }else {
            IPage<UserEntity> page = new Page<>();
            return new PageUtils(page);
        }

    }

    public int delUserTeamInfoById(int userId){
        return userTeamDao.delUserTeamInfoById(userId);
    }

}